﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Etapa1.BindKey;
using Etapa1.Objects;
using PloobsEngine.Physics;
using PloobsEngine.SceneControl;
using PloobsEngine.Input;
using PloobsEngine.Modelo;
using Microsoft.Xna.Framework;
using PloobsEngine.Material;
using PloobsEngine.Cameras;
using PloobsEngineDemo3;
using PloobsEngine;
using PloobsEngine.Particle3D;
using PloobsEngine.Draw;
using PloobsEngine.Loader;
using PloobsEngine.Light;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Physics.Bepu;

namespace Etapa1.Screens
{
    /// <summary>
    /// Particle Screen
    /// </summary>
    public class ParticlesScreen : SceneScreen
    {
        private BepuPhysicWorld physicWorld;
        private BrutalForceWorld mundo;
        private EngineStuff engine;        
        ICamera cam;

        /// <summary>
        /// Sistemas de particulas q serao utilizados
        /// </summary>
        ParticleSystem explosionParticles;
        ParticleSystem explosionSmokeParticles;
        ParticleSystem projectileTrailParticles;
        ParticleSystem smokePlumeParticles;
        ParticleSystem fireParticles;        

        public ParticlesScreen(EngineStuff es)
        {
            this.engine = es;
            physicWorld = new BepuPhysicWorld();            
            mundo = new BrutalForceWorld(physicWorld);                                    
        }
        
        public override void LoadContent(EngineStuff es)
        {            
            InputAdvanced inp = new InputAdvanced(engine);
            engine.AddComponent(inp);

            base.LoadContent(es);

            ///Cria os sistemas de particulas
            explosionParticles = new ExplosionParticleSystem(engine);
            explosionSmokeParticles = new ExplosionSmokeParticleSystem(engine);
            projectileTrailParticles = new ProjectileTrailParticleSystem(engine);
            smokePlumeParticles = new SmokePlumeParticleSystem(engine);
            fireParticles = new FireParticleSystem(engine);

            ///Adiciona cada um ao manager
            mundo.ParticleManager.AddParticleSystem(explosionParticles);
            mundo.ParticleManager.AddParticleSystem(explosionSmokeParticles);
            mundo.ParticleManager.AddParticleSystem(projectileTrailParticles);
            mundo.ParticleManager.AddParticleSystem(smokePlumeParticles);
            mundo.ParticleManager.AddParticleSystem(fireParticles);

            ///Cria um emissor de particulas
            BombsEmiter me = new BombsEmiter(new Vector3(-20, 10,550));
            ///Adiciona ele no mundo
            mundo.ParticleManager.AddEmiter(me);
            
            //CircleOfFireEmiter cofe = new CircleOfFireEmiter(new Vector3(-20,30,50));
            //mundo.ParticleManager.AddEmiter(cofe);

            SmokeEmiter se = new SmokeEmiter(new Vector3(50, 10, 150));
            mundo.ParticleManager.AddEmiter(se);

            #region Models
            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\cubo", "..\\Content\\Textures\\white");
                sm.LoadModelo();
                BoxObject pi = new BoxObject(new Vector3(50, 10, 150), 1, 5, new Vector3(50));
                CustomDeferred shader = new CustomDeferred(false, false, false, false);                
                shader.Initialize();
                IMaterial mat = new Material(shader);
                IObject obj3 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj3);
            }
            ///Modelo de sempre
            {                
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\cenario");
                sm.LoadModelo();
                IPhysicObject pi = new TriangleMeshObject(Vector3.Zero,sm);
                pi.isMotionLess = true;
                NormalDeferred shader = new NormalDeferred();
                shader.Initialize();
                IMaterial mat = new Material(shader);
                IObject obj4 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj4);
            }
            
            #endregion
      
            cam = new CameraFirstPerson(true);            
            cam.FarPlane = 3000;

            LightThrowBepu lt = new LightThrowBepu(mundo, engine);         

            #region NormalLight
            DirectionalLight ld1 = new DirectionalLight(Vector3.Left, Color.White);
            DirectionalLight ld2 = new DirectionalLight(Vector3.Right, Color.White);
            DirectionalLight ld3 = new DirectionalLight(Vector3.Backward, Color.White);
            DirectionalLight ld4 = new DirectionalLight(Vector3.Forward, Color.White);
            DirectionalLight ld5 = new DirectionalLight(Vector3.Down, Color.White);
            float li = 0.4f;
            ld1.LightIntensity = li;
            ld2.LightIntensity = li;
            ld3.LightIntensity = li;
            ld4.LightIntensity = li;
            ld5.LightIntensity = li;
            mundo.AddLight(ld1);
            mundo.AddLight(ld2);
            mundo.AddLight(ld3);
            mundo.AddLight(ld4);
            mundo.AddLight(ld5);
            #endregion

            ////Criacao de uma Tecnica
            DeferredRenderTechnic dr = new DeferredRenderTechnic(engine);            
            IRenderTechnic[] rt = new IRenderTechnic[] { dr };
            this.RenderTechnics = rt;
            mundo.AddCamera(cam);
            this.World = mundo;

        }
    }
}

